
#include "define.h"


class CBuff
{
	enum
	{
		DEF_DEFAULT_SIZE = 1024,
	};
public:
	CBuff( size_t nSize = DEF_DEFAULT_SIZE )
	{
		buff = new char[nSize];		
		m_nLimitSize = nSize;

		clear();		
	}

	~CBuff() { SAFE_DELETE( buff ); }



	void reset() { cur = buff; }
	void clear() 
	{
		memset( buff, 0, m_nLimitSize );
		m_nSize = 0;

		reset();
	}

	size_t getSize() const { return m_nSize; }
	
	int put( void* data, size_t size = 0 )
	{
		//*((int*)cur) = *data;

		if( 0 == size )
			size = sizeof(data);

		if( m_nLimitSize <= (getSize()+size) )
			return -1;

		memcpy( cur, (char*)data, sizeof(data) );		
		cur += size;
		
		m_nSize += sizeof(data);
		
		return m_nSize;
	}
	
	
	int get( void* data, size_t size = 0 )
	{
		if( 0 == size )
			size = sizeof(data);

		memcpy( data, cur, sizeof(data) );
		cur += size;

		return size;
	}

	void close()
	{
		SAFE_DELETE( buff );
	}
	
private:
	char* buff;
	char* cur;
	size_t m_nSize;
	size_t m_nLimitSize;
};
